<?php
class ASolicitudPartesAutomatica
{
	public function __construct() {}
	public function generarOrdenDespachoPartes(EFacade & $facade) 
	{
		$m = epManager::instance();
		$docSPA = & $facade->getDocumentRoot();

		/********** LIBERAR LA RESERVA *************/
		$ezpdo_ = getManager();
		$consulta = "from MReservaFuncionalidad as t where t.solicitudPartes = ?";
		$reservaciones = $ezpdo_->find($consulta,$docSPA);
		foreach($reservaciones as $reservacion){
			//echo "<hr>reserva: ",$reservacion;
			$ezpdo_->delete($reservacion);
		}
		/*******************************************/
		
		//Creo la ODP
		$ODP = $facade->session->create('DOrdenDespachoPartes');
		$area = $m->find('FROM MAreaTrabajo as t WHERE t.nombre=?','Mantenimiento');
		$ODP->areaTrabajo = $area[0];
		$ODP->instanciaEquipo = $docSPA->instanciaEquipo;
		$ODP->solicitud = $docSPA;
		$role = null;
		$newEfacade = & EFacade :: initWorkflow($facade->session, $ODP, & $role);
		$transSet = & EFacade :: getCurrentTransSetAuto($newEfacade->getWorkflow(), /*PROCESS */ 3);
		$trans = $transSet->getTransByName('t1');
		$newEfacade->fireTrans($trans, $role, "Crear",TRUE);
		$ODP->epSetDirty(TRUE);// marcar el documento para que sea salvado los cambios en su lista de items
		$newEfacade->saveWorkflow();
		$ODP->commit();
		$ODP->numero = $ODP->getDocumentNumber();
		unset($newEfacade);
		//**********************************************************
		
		//Disparar la OM
		$OM = $docSPA->ordenGenerada;
		$workflow = & $OM->getWorkflowRef();
		$facade->session->refresh($workflow);
		$newEfacade = & EFacade :: initWorkflowWithoutRole($facade->session, $OM, 3);//tipo Process
		$transSet = & EFacade :: getCurrentTransSetAuto($workflow, /*PROCESS */ 3);
		$trans = $transSet->getTransByName('t12');
		$role = null;
		//Solo si esta disponible la t15
		if($trans){
			$newEfacade->fireTrans($trans, $role, "Culmina",TRUE);
			$workflow->epSetDirty(TRUE);
			$OM->epSetDirty(TRUE);
			$newEfacade->saveWorkflow();
		}
				
		//Disparo el agente en t3 de la SPA
		$workflowSPA = & $facade->getWorkflow();
		$transSetSPA = & EFacade :: getCurrentTransSetAuto($workflowSPA, 2); //TYPE AUTO AGENT
		$role=NULL;
		$transSA = $transSetSPA->getTransByName('t3');
		$facade->fireTrans($transSA,$role,'Gen ODP', TRUE);
	}
	public function culminarSolicitud(EFacade & $facade) 
	{
		$m = epManager::instance();
		$SPA = & $facade->getDocumentRoot();
				
		//Disparar la OM
		$OM = $SPA->ordenGenerada;
		$workflow = & $OM->getWorkflowRef();
		$facade->session->refresh($workflow);
		$newEfacade = & EFacade :: initWorkflowWithoutRole($facade->session, $OM, 3);//tipo Process
		$transSet = & EFacade :: getCurrentTransSetAuto($workflow, /*PROCESS */ 3);
		$trans = $transSet->getTransByName('t15');
		$role = null;
		//Solo si esta disponible la t15
		if($trans){
			$newEfacade->fireTrans($trans, $role, "Culmina",TRUE);
			$workflow->epSetDirty(TRUE);
			$OM->epSetDirty(TRUE);
			$newEfacade->saveWorkflow();
		}

		//Disparo el agente en t5 de la SPA
		$workflowSPA = & $facade->getWorkflow();
		$transSetSPA = & EFacade :: getCurrentTransSetAuto($workflowSPA, 2); //TYPE AUTO AGENT
		$role=NULL;
		$transSA = $transSetSPA->getTransByName('t5');
		$facade->fireTrans($transSA,$role,'Culmina', TRUE);
	}
	public function culminarOrdenDespachoPartes(EFacade & $facade) 
	{
		$m = epManager::instance();
		$SPA = & $facade->getDocumentRoot();
				
		/////////////Disparar la ODP////////////////////////////////////
		$ODP = $SPA->despacho;
		$workflow = & $ODP->getWorkflowRef();
		$facade->session->refresh($workflow);
		$newEfacade = & EFacade :: initWorkflowWithoutRole($facade->session, $ODP, 3);//tipo Process
		$transSet = & EFacade :: getCurrentTransSetAuto($workflow, /*PROCESS */ 3);
		$trans = $transSet->getTransByName('t7');
		$role = null;
		if($trans)
			$newEfacade->fireTrans($trans, $role, "Culmina",TRUE);
		else{
			$trans = $transSet->getTransByName('t8');
			if($trans)
				$newEfacade->fireTrans($trans, $role, "Culmina",TRUE);
			else{
				//Disparo la transicion 15 ya que la 12 ya fue disparada
				$trans = $transSet->getTransByName('t9');
				$newEfacade->fireTrans($trans, $role, "Culmina",TRUE);
			}
		}
		$workflow->epSetDirty(TRUE);
		$OM->epSetDirty(TRUE);
		$newEfacade->saveWorkflow();
		
		///////Disparo el agente t9 de la SPA////////////////////
		$workflowSPA = & $facade->getWorkflow();
		$transSetSPA = & EFacade :: getCurrentTransSetAuto($workflowSPA, 2); //TYPE AUTO AGENT
		$role=NULL;
		$transSA = $transSetSPA->getTransByName('t9');
		$facade->fireTrans($transSA,$role,'Culmina', TRUE);
	}		
}
?>